import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';

//記得使用Catch裝飾器，可以傳入無數個參數。
@Catch(HttpException)
//建議要implements ExceptionFilter，實作它的interface
export class CustomExceptionFilter implements ExceptionFilter {
	//傳入HttpException
	catch(exception: HttpException, host: ArgumentsHost) {
		const ctx = host.switchToHttp();
		const response = ctx.getResponse<Response>();
		const request = ctx.getRequest<Request>();

		// msg
		const status = exception.getStatus();
		const message = '我是Exception Log Message';

		//做log動作
		console.log(`exception status:`, status);
		console.log(`exception message:`, message);

		//調整response的json內容
		response.status(200).json({
			code: status,
			msg: message,
		});
	}
}